﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using UFIDA.U8.DB;

namespace AnLianSysClient
{
    class DAL_custwork_zj_avg
    {
        public static List<custwork_zj_avg> GetModel(int year, int month, string corp, string conn)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from vw_custwork_zj_avg ");
            strSql.Append(" where  iyear=@iyear and imonth=@imonth and corp = @corp ");
            SqlParameter[] parameters = {
					new SqlParameter("@iyear", SqlDbType.Int,4),
					new SqlParameter("@imonth", SqlDbType.Int,4),
                    new SqlParameter("@corp", SqlDbType.VarChar,20)
                                            };
            parameters[0].Value = year;
            parameters[1].Value = month;
            parameters[2].Value = corp;

            List<custwork_zj_avg> mlist = new List<custwork_zj_avg>();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), conn, parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow item in ds.Tables[0].Rows)
                {
                    mlist.Add(DataRowToModel(item));
                }
                return mlist;
            }
            else
            {
                return null;
            }
        }

        private static custwork_zj_avg DataRowToModel(DataRow row)
        {
            custwork_zj_avg model = new custwork_zj_avg();
            Type examType = typeof(custwork_zj_avg);
            if (row != null)
            {
                DataColumnCollection dc = row.Table.Columns;
                for (int i = 0; i < dc.Count; i++)
                {
                    if (row[dc[i].ColumnName] != null && row[dc[i].ColumnName].ToString() != "")
                    {
                        PropertyInfo piInstance = examType.GetProperty(dc[i].ColumnName);
                        if (piInstance != null)
                        {
                            if (row[dc[i].ColumnName] != DBNull.Value)
                            {
                                piInstance.SetValue(model, Convert.ChangeType(row[dc[i].ColumnName], piInstance.PropertyType), null);
                            }
                            else
                            {
                                piInstance.SetValue(model, null, null);
                            }
                        }
                    }
                }
            }
            return model;
        }
    }
    class custwork_zj_avg
    {
        public string imonth { get; set; }
        public string project { get; set; }
        public string projectname { get; set; }
        public string projectclass { get; set; }
        public string ccode { get; set; }
        public string ccodename { get; set; }
        public string cdeptcode { get; set; }
        public decimal jf { get; set; } 
        public decimal df { get; set; }
        public string corp { get; set; } 
        public string ccode2 { get; set; }
    }
}